# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json

name: Publish .NET app to GitHub Releases

env:
  pack: FtpsFileMirror
  proj: FtpsClient
  url: ${{ github.server_url }}/${{ github.repository }}

on:
  push:
    tags:
      - "v*.*.*"
  #release:
  #  types: [published]
  workflow_dispatch:

jobs:
  release:
    name: Release
    strategy:
      matrix:
        kind: ['windows', 'linux', 'macOS']
        include:
          - kind: windows
            os: windows-latest
            target: win-x64
          - kind: linux
            os: ubuntu-latest
            target: linux-x64
          - kind: macOS
            os: macos-latest
            target: osx-x64
    runs-on: ${{ matrix.os }}
    permissions:
      contents: write
      packages: write
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Setup dotnet
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 7.0.x

      - name: Build
        shell: bash
        run: |
          tag=$(git describe --tags --abbrev=0)
          release_name="${{ env.pack }}-$tag-${{ matrix.target }}"
          # Build everything
          dotnet publish ${{ env.proj }}/${{ env.proj }}.csproj --runtime "${{ matrix.target }}" --no-self-contained -c Release -o "$release_name"
          echo Source: ${{ env.url }} > "${release_name}/readme.txt"
          # Pack files
          if [ "${{ matrix.target }}" == "win-x64" ]; then
            7z a -tzip "${release_name}.zip" "./${release_name}/*"
          else
            tar czvf "${release_name}.tar.gz" "$release_name"
          fi
          # Delete output directory
          rm -r "$release_name"
      - name: Publish
        uses: softprops/action-gh-release@v1
        with:
          files: ${{ env.pack }}-*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
